//
//  VisitorView.swift
//  WeiBo_One
//
//  Created by apple on 15/10/19.
//  Copyright © 2015年 蜡笔小炎. All rights reserved.
//

import UIKit

class VisitorView: UIView {

//-----------------------------------------------------------------
    // MARK: - 懒加载控件
    
    //小房子
    private lazy var homeView: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_house")
    //注册按钮
    lazy var registBtn: UIButton = UIButton(title: "注册", color: UIColor.orangeColor(), fontSize: 15, backImageName: "common_button_white_disable")
    //登录按钮
    lazy var loginBtn: UIButton = UIButton(title: "登录", color: UIColor.lightGrayColor(), fontSize: 15, backImageName: "common_button_white_disable")
    //消息文字
    private lazy var messageLabel: UILabel = {
        
        let label = UILabel()
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.font = UIFont.systemFontOfSize(14)
        label.numberOfLines = 0
        label.textAlignment = NSTextAlignment.Center
        return label
    }()
    //花圈
    private lazy var flowerRoundView: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_smallicon")
    //遮罩视图
    private lazy var maskIconView: UIImageView = UIImageView(imageName: "visitordiscover_feed_mask_smallicon")
    
//-----------------------------------------------------------------
    // MARK: - 加载视图
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
    
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
        loadUI()
    }

    required init?(coder aDecoder: NSCoder) {
        
        super.init(coder: aDecoder)
        
        loadUI()
        // 由于遮罩图片整体效果不好，添加灰色背景
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
    }
    
    /// 设置界面布局
    private func loadUI() {
        addSubview(flowerRoundView)
        addSubview(maskIconView)
        addSubview(homeView)
        addSubview(registBtn)
        addSubview(loginBtn)
        addSubview(messageLabel)
        
        // 将所有控件关闭frame设置，开启自动布局
        for v in subviews{
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        // 添加所有控件的自动布局
        addAllConstraints()
    }
    
    /// 接收数据，为tabbar根控制器加载各自的游客视图
    func setupInfo(imageName: String?, message: String) {

        messageLabel.text = message
        guard let imageName = imageName else {
            //如果传入的imageName为空，开启花圈动画
            startAnimat()
            return
        }
        //如果传入的imageName不为空
        flowerRoundView.image = UIImage(named: imageName)  //替换花圈图片为需要的图片
        homeView.hidden = true  //隐藏小房子的图片
        sendSubviewToBack(maskIconView) //将遮罩视图移至底层
    }
    
    /// 开启花圈动画
    func startAnimat() {
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")//旋转
        anim.toValue = 2 * M_PI
        anim.duration = 20
        anim.repeatCount = MAXFLOAT
        anim.removedOnCompletion = false    //动画永不停止
        
        flowerRoundView.layer.addAnimation(anim, forKey: nil)
    }
    
}

// MARK: - 控件的自动布局
extension VisitorView {
    private func addAllConstraints() {
        // 1> 花圈
        addConstraint(NSLayoutConstraint(item: flowerRoundView,
            attribute: .CenterX,
            relatedBy: .Equal,
            toItem: self,
            attribute: .CenterX,
            multiplier: 1.0,
            constant: 0))
        addConstraint(NSLayoutConstraint(item: flowerRoundView,
            attribute: .CenterY,
            relatedBy: .Equal,
            toItem: self,
            attribute: .CenterY,
            multiplier: 1.0,
            constant: -60))
        // 2> 小房子
        addConstraint(NSLayoutConstraint(item: homeView,
            attribute: .CenterX,
            relatedBy: .Equal,
            toItem: flowerRoundView,
            attribute: .CenterX,
            multiplier: 1.0,
            constant: 0))
        addConstraint(NSLayoutConstraint(item: homeView,
            attribute: .CenterY,
            relatedBy: .Equal,
            toItem: flowerRoundView,
            attribute: .CenterY,
            multiplier: 1.0,
            constant: 0))
        
        // 3> 消息文字
        addConstraint(NSLayoutConstraint(item: messageLabel,
            attribute: .CenterX,
            relatedBy: .Equal,
            toItem: flowerRoundView,
            attribute: .CenterX,
            multiplier: 1.0,
            constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel,
            attribute: .Top,
            relatedBy: .Equal,
            toItem: flowerRoundView,
            attribute: .Bottom,
            multiplier: 1.0,
            constant: 16))
        addConstraint(NSLayoutConstraint(item: messageLabel,
            attribute: .Width,
            relatedBy: .Equal,
            toItem: nil,
            attribute: .NotAnAttribute,
            multiplier: 1.0,
            constant: 224))
        addConstraint(NSLayoutConstraint(item: messageLabel,
            attribute: .Height,
            relatedBy: .Equal,
            toItem: nil,
            attribute: .NotAnAttribute,
            multiplier: 1.0,
            constant: 36))
        
        // 4> 注册按钮
        addConstraint(NSLayoutConstraint(item: registBtn,
            attribute: .Left,
            relatedBy: .Equal,
            toItem: messageLabel,
            attribute: .Left,
            multiplier: 1.0,
            constant: 0))
        addConstraint(NSLayoutConstraint(item: registBtn,
            attribute: .Top,
            relatedBy: .Equal,
            toItem: messageLabel,
            attribute: .Bottom,
            multiplier: 1.0,
            constant: 16))
        addConstraint(NSLayoutConstraint(item: registBtn,
            attribute: .Width,
            relatedBy: .Equal,
            toItem: nil,
            attribute: .NotAnAttribute,
            multiplier: 1.0,
            constant: 100))
        addConstraint(NSLayoutConstraint(item: registBtn,
            attribute: .Height,
            relatedBy: .Equal,
            toItem: nil,
            attribute: .NotAnAttribute,
            multiplier: 1.0,
            constant: 35))
        // 5> 登陆按钮
        addConstraint(NSLayoutConstraint(item: loginBtn,
            attribute: .Right,
            relatedBy: .Equal,
            toItem: messageLabel,
            attribute: .Right,
            multiplier: 1.0,
            constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn,
            attribute: .Top,
            relatedBy: .Equal,
            toItem: messageLabel,
            attribute: .Bottom,
            multiplier: 1.0,
            constant: 16))
        addConstraint(NSLayoutConstraint(item: loginBtn,
            attribute: .Width,
            relatedBy: .Equal,
            toItem: nil,
            attribute: .NotAnAttribute,
            multiplier: 1.0,
            constant: 100))
        addConstraint(NSLayoutConstraint(item: loginBtn,
            attribute: .Height,
            relatedBy: .Equal,
            toItem: nil,
            attribute: .NotAnAttribute,
            multiplier: 1.0,
            constant: 35))
        // 6> 遮罩视图
        let viewDict = ["maskIconView": maskIconView, "registBtn": registBtn]
        let metricDict = ["buttonHeight": -35]
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(
            "H:|-0-[maskIconView]-0-|",
            options: [],
            metrics: nil,
            views: viewDict))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(
            "V:|-0-[maskIconView]-(buttonHeight)-[registBtn]",
            options: [],
            metrics: metricDict,
            views: viewDict))
    }
}
